package com.dcloud.streamweb.sysconfig.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dcloud.streamweb.enums.SysConfigEnumType;
import com.dcloud.streamweb.exception.BizException;
import com.dcloud.streamweb.sysconfig.entity.req.SysConfigUpdateReq;
import com.dcloud.streamweb.sysconfig.entity.vo.SystemConfigVO;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import com.dcloud.streamweb.sysconfig.entity.SystemConfig;
import com.dcloud.streamweb.sysconfig.service.ISystemConfigService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

 /**
 * @Description: system_config
 * @Author: jeecg-boot
 * @Date:   2024-07-11
 * @Version: V1.0
 */
@Api(tags="系统设置")
@RestController
@RequestMapping("/api/systemConfig/v1")
@Slf4j
public class SystemConfigController extends JeecgController<SystemConfig, ISystemConfigService> {
	@Autowired
	private ISystemConfigService systemConfigService;


	 /**
	  * @Description: 查询系统配置列表
	  * @param
	  * @return: Result<?>
	  * @Author: shijf
	  * @Date 2024/7/16 15:49
	  */
	 @ApiOperation(value="系统设置列表", notes="系统设置列表")
	 @PostMapping(value = "/sysConfig")
	 public Result<?> sysConfig() {


		 List<SystemConfigVO> list = SystemConfigVO
				 .toListVO(systemConfigService.getSystemConfig(SysConfigEnumType.SYS));
		 return Result.ok(list);
	 }

	 /**
	  * @Description: 修改系统配置
	  * @param
	  * @return: Result<?>
	  * @Author: shijf
	  * @Date 2024/7/16 15:49
	  */
	 @ApiOperation(value="系统设置修改", notes="系统设置修改")
	 @PostMapping(value = "/update")
	 public Result<?> update(@RequestBody SysConfigUpdateReq sysConfigUpdateReq) {
		 try {
		 	systemConfigService.addOrUpdateConfigByKey(sysConfigUpdateReq.getKey(), sysConfigUpdateReq.getVal().trim());
		 } catch (BizException biz) {
			 log.warn("upsertSynConfig is error ", biz);
			 return Result.error(biz.getMessage());
		 } catch (Exception e) {
			 log.error("upsertSynConfig is error", e);
			 return Result.error(e.getMessage());
		 }
		 return Result.ok();
	 }


	 /**
	  * @Description: 删除系统配置
	  * @param
	  * @return: Result<?>
	  * @Author: shijf
	  * @Date 2024/7/16 15:49
	  */
	 @ApiOperation(value="系统设置修改", notes="系统设置修改")
	 @DeleteMapping(value = "/delete")
	 public Result<?> delete(@RequestParam(name="key",required=true) String key) {
		 try {
			 systemConfigService.deleteConfigByKey(key);
		 } catch (BizException biz) {
			 log.warn("deleteSynConfig is error ", biz);
			 return Result.error(biz.getMessage());
		 } catch (Exception e) {
			 log.error("deleteSynConfig is error", e);
			 return Result.error(e.getMessage());
		 }
		 return Result.ok();
	 }

}
